package com.yjd.comm.util;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * <b>description</b>：开发api工具类 <br>
 * <b>time</b>：2015-4-22下午3:48:10 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class OpenApiUtil {

	public static final String MEMBER_KEY = "OPEN_API.MEMBER_KEY";

	/**
	 * 验证签名是否有效
	 * 
	 * @param paramMap
	 * @param sign
	 * @return
	 */
	public static boolean validSign(Map<Object, Object> paramMap, String sign) {
		String member_key = getMemberKey();
		paramMap.put("member_key", member_key);
		boolean result = createSign(paramMap).equals(sign);
		paramMap.remove("member_key");
		return result;
	}
	
	public static String getMemberKey(){
		return FrameUtil.getConstResource(MEMBER_KEY); 
	}

	/**
	 * 生成签名
	 * 
	 * @param paramMap
	 * @return
	 */
	public static String createSign(Map<Object, Object> paramMap) {
		StringBuilder sb = new StringBuilder();
		for (Entry<Object, Object> entry : paramMap.entrySet()) {
			sb.append(entry.getValue());
		}
		return DigestUtils.md5Hex(sb.toString());
	}
}
